package cn.compose.online.controller;

import cn.compose.online.biz.TestBiz;
import cn.compose.online.entity.Area;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import cn.compose.common.enums.ExecutorType;
import cn.compose.common.exception.runtime.ExecuteException;
import cn.compose.runtime.Executor;
import cn.compose.runtime.ExecutorContext;
import cn.compose.runtime.ExecutorFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class TestController {
    @Resource
    private ExecutorFactory executorFactory;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private TestBiz testBiz;

    @RequestMapping("/test")
    public Object test(@RequestParam String code, @RequestParam String method) throws ExecuteException {
        Executor executor = executorFactory.getExecutor(ExecutorType.GROOVY);
        ExecutorContext executeContext = new ExecutorContext();
        executeContext.setCode(code);
        executeContext.setDebug(true);
        executeContext.setMethodName(method);
        Object execute = executor.execute(executeContext);

        return execute;
    }

    @GetMapping("/redisTest")
    public Object testRedis(){
        redisTemplate.opsForValue().set("redisTest","test");
        List<Area> areaList=testBiz.listArea();
        System.err.println(JSON.toJSON(areaList));
        return redisTemplate.opsForValue().get("redisTest");
    }

    public static void main(String[] args) throws ExecuteException {

        String s="class Geet{\n" +
                "    public String execute(Map<String, String[]> map) {\n" +
                "    long rsv46=1432778;\n" +
                "    def rsvField46= rsv46;\n" +
                "    //def rsvField46= N_5332_1.rsv46;\n" +
                "    if(rsvField46==null){\n" +
                "        return null;\n" +
                "    }\n" +
                "    //接收到数字类型数据，默认都是10进制数据，多字段组合时需转换为16进制组合\n" +
                "\n" +
                "    //转换位16进制进行取位---rsvField46为十进制数据\n" +
                "    String str46=String.format(\"%06X\",rsvField46);\n" +
                "    StringBuilder stringBuilder=new StringBuilder();\n" +
                "    String character = \".\";\n" +
                "    // 取位组合 - 取位后的数据需将16进制数据转为10进制组合显示\n" +
                "    stringBuilder.append(new BigInteger(str46.substring(0,2),16).intValue())\n" +
                "    .append(character)\n" +
                "    .append(new BigInteger(str46.substring(2,4),16).intValue())\n" +
                "    .append(character)\n" +
                "    .append(new BigInteger(str46.substring(4,6),16).intValue())\n" +
                "\n" +
                "    String result10=stringBuilder.toString();\n" +
                "    println(\"rsv46-10进制: \"+result10)\n" +
                "    return result10;\n" +
                "}\n" +
                "    \n" +
                "}";

        String code = "package com.tianxiao.faas.application.web;\n" +
                "import java.util.Map;\n" +
                "import java.util.Set;\n" +
                "\n" +
                "public class Tests {\n" +
                "    public String execute(Map<String, String[]> map) {\n" +
                "        StringBuilder result = new StringBuilder();\n" +
                "\n" +
                "        Set<String> strings = map.keySet();\n" +
                "        for (String key : strings) {\n" +
                "            result.append(key + \"|\").append(map.get(key)).append(\"-\");\n" +
                "        }\n" +
                "        User user = new User();\n" +
                "        user.sayHello();\n" +
                "\n" +
                "   int i = 1;\n"+
                "        while (i<10) {\n" +
                " i++; \n"+
                "            System.out.println(1);\n" +
                "        }\n" +
                "\n" +
                "    }\n" +
                "\n" +
                "    public static class User {\n" +
                "        public void sayHello() {\n" +
                "            System.out.println(\"hello\");\n" +
                "        }\n" +
                "    }\n" +
                "}";

        System.err.println(s);
        ExecutorFactory executorFactory = ExecutorFactory.getInstance();
        executorFactory.init();
        Executor executor = executorFactory.getExecutor(ExecutorType.GROOVY);
        ExecutorContext executeContext = new ExecutorContext();
        executeContext.setCode(s);
        executeContext.setDebug(true);

        executeContext.setParams(Lists.newArrayList(Maps.newHashMap()));
        executeContext.setMethodName("execute");
        Object execute = executor.execute(executeContext);
        System.out.println(execute);

        String str="class Geet{\n" +
                "    static void main(String[] args){\n" +
                "        Geet geet = new Geet();\n" +
                "        geet.rsv46();\n" +
                "    }\n" +
                "    \n" +
                "    String rsv46(){\n" +
                "    long rsv46=1432778;\n" +
                "    def rsvField46= rsv46;\n" +
                "    //def rsvField46= N_5332_1.rsv46;\n" +
                "    if(rsvField46==null){\n" +
                "        return null;\n" +
                "    }\n" +
                "    //接收到数字类型数据，默认都是10进制数据，多字段组合时需转换为16进制组合\n" +
                "\n" +
                "    //转换位16进制进行取位---rsvField46为十进制数据\n" +
                "    String str46=String.format(\"%06X\",rsvField46);\n" +
                "    StringBuilder stringBuilder=new StringBuilder();\n" +
                "    String character = \".\";\n" +
                "    // 取位组合 - 取位后的数据需将16进制数据转为10进制组合显示\n" +
                "    stringBuilder.append(new BigInteger(str46.substring(0,2),16).intValue())\n" +
                "    .append(character)\n" +
                "    .append(new BigInteger(str46.substring(2,4),16).intValue())\n" +
                "    .append(character)\n" +
                "    .append(new BigInteger(str46.substring(4,6),16).intValue())\n" +
                "\n" +
                "    String result10=stringBuilder.toString();\n" +
                "    println(\"rsv46-10进制: \"+result10)\n" +
                "    return result10;\n" +
                "}\n" +
                "    \n" +
                "}";
    }
}
